package com.xinchao.service.impl;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.xinchao.constant.AdvertisementConstant;
import com.xinchao.constant.DateConstant;
import com.xinchao.constant.RequestParameterConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.Advertiser;
import com.xinchao.model.vo.ChargeVO;
import com.xinchao.service.*;
import com.xinchao.util.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 * @date 2018/11/12 16:58
 */
@Service(value = "advertisementMonitorChargeService")
public class AdvertisementMonitorChargeServiceImpl implements AdvertisementMonitorChargeService {
	@Autowired
	private AdvertisementMonitorService advertisementMonitorService;
	@Autowired
	private AdvertiserService advertiserService;
	@Autowired
	private AdvertisementService advertisementService;
	@Autowired
	private ChargeService chargeService;
	/**
	 * 获取广告消费金额列表
	 * @param advertisementName 广告名称
	 * @param timeType          时间类型
	 * @return List<ChargeVO>
	 * @throws ServiceException
	 */
	@Override
	public List<ChargeVO> listChargeVO(String advertisementName, Integer timeType) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//开始时间
		Long startTime = null;
		//结束时间
		Long endTime = null;
		//时间列表
		List<String> dayList = new ArrayList<>();
		//近7天
		if (RequestParameterConstant.TIME_TYPE_NEAR_SEVEN_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(7, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//近30天
		if (RequestParameterConstant.TIME_TYPE_NEAR_THIRTY_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(30, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//消费VO列表
		List<ChargeVO> voList = new ArrayList<>();
		for (String day : dayList) {
			startTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			endTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MAX, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			Long watchSum = advertisementMonitorService.sumWatch(advertisementName, startTime, endTime);
			if (watchSum == null) {
				continue;
			}
			ChargeVO vo = new ChargeVO();
			vo.setTime(day);
			//广告A的消费金额=广告A的观看人次*￥0.01
			System.out.println("watchSum: " + watchSum);
			BigDecimal consumedCharge = new BigDecimal(watchSum).multiply(AdvertisementConstant.PER_ADVERTISEMENT_CHARGE);
			//保留两位
			double doubleValue = consumedCharge.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			vo.setConsumedCharge(doubleValue);
			voList.add(vo);
		}
		return voList;
	}

	/**
	 * 获取广告时段消费金额列表
	 * @param advertisementName 广告名称
	 * @param date              日期
	 * @return List<ChargeVO>
	 * @throws ServiceException
	 */
	@Override
	public List<ChargeVO> listTimeFrameChargeVO(String advertisementName, String date) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || StringUtils.isBlank(date)) {
			throw new ServiceException("输入参数不能为空");
		}
		//日期
		Long startDate = DateUtils.stringTimeToLongTimeStamp(date, DateConstant.DATE_SHORT_ISO);
		//时间段列表
		List<String> timeFrameList = DateUtils.generateTimeFrameOfDay(startDate);
		Map<String, String> dayTimeFrameMap = DateConstant.DAY_TIME_FRAME_MAP;
		//结束日期
		Long endDate = null;
		//消费VO列表
		List<ChargeVO> voList = new ArrayList<>();
		for (int i = 0; i < timeFrameList.size(); i++) {
			String timeFrame = timeFrameList.get(i);
			//完整的结束日期
			endDate = DateUtils.stringTimeToLongTimeStamp(date + " " + timeFrame + ":00", DateConstant.LONG_DATE_FORMAT_STR);
			Long watchSum = advertisementMonitorService.sumWatch(advertisementName, startDate, endDate);
			ChargeVO vo = new ChargeVO();
			vo.setTime(timeFrame);
			//消费金额
			if (watchSum == null){
				vo.setConsumedCharge(0);
			} else {
				//广告A的消费金额=广告A的观看人次*￥0.01
				BigDecimal consumedCharge = new BigDecimal(watchSum).multiply(AdvertisementConstant.PER_ADVERTISEMENT_CHARGE);
				//精确到两位数
				double value = consumedCharge.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				vo.setConsumedCharge(value);
			}
			voList.add(vo);
			//重新计算开始日期
			String startDateStr = dayTimeFrameMap.get(timeFrame);
			startDate = DateUtils.stringTimeToLongTimeStamp(date + " " + startDateStr, DateConstant.LONG_DATE_FORMAT_STR);
		}
		return voList;
	}

	/**
	 * 获取用户余额
	 * @param userName 用户名
	 * @return Double
	 * @throws ServiceException
	 */
	@Override
	public Double getUserBalance(String userName) throws ServiceException {
		if (StringUtils.isBlank(userName)) {
			throw new ServiceException("用户名不能为空");
		}
		//获取用户的广告主
		Advertiser advertiser = advertiserService.getAdvertiserByUserName(userName);
		if (advertiser == null) {
			throw new ServiceException("用户不存在，请联系管理员");
		}
		//广告主ID
		Long advertiserId = advertiser.getAdvertiserId();
		//获取广告主得总费用
		BigDecimal totalCharge = chargeService.getTotalChargeByAdvertiserId(advertiserId);
		if (totalCharge == null) {
			return 0D;
		}
		//获取广告主所有拥有的广告编码
		List<String> advertisementCodeList = advertisementService.listAdvertisementCode(advertiserId);
		if (CollectionUtils.isEmpty(advertisementCodeList)) {
			return totalCharge.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		//获取所用广告的总观看次数
		Long sumWatch = advertisementMonitorService.sumWatchNumber(advertisementCodeList);
		if (sumWatch == null) {
			return totalCharge.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		//消费金额=广告观看人次*￥0.01
		BigDecimal consumedCharge = new BigDecimal(sumWatch).multiply(AdvertisementConstant.PER_ADVERTISEMENT_CHARGE);
		return totalCharge.subtract(consumedCharge).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}
